#include <rclcpp/rclcpp.hpp>
#include <rclcpp_lifecycle/lifecycle_node.hpp>
#include <chrono>
#include <iostream>

class TemplateLifecycleNode : public rclcpp_lifecycle::LifecycleNode
{
public:
    TemplateLifecycleNode() : rclcpp_lifecycle::LifecycleNode("template_lifecycle_node") {
        //不依赖参数的初始化
    }
    ~TemplateLifecycleNode(){}

private:

    // 配置回调
    rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
    on_configure(const rclcpp_lifecycle::State & previous_state) override
    {
        // 初始化操作，如读取参数、创建订阅者和发布者等
        RCLCPP_INFO(get_logger(), "Node is configuring.");

        return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS;
    }

    // 激活回调
    rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
    on_activate(const rclcpp_lifecycle::State & previous_state) override
    {
        // 启动定时器，开始执行周期性任务
        RCLCPP_INFO(get_logger(), "Node is activating.");

        return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS;
    }

    // 非激活回调
    rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
    on_deactivate(const rclcpp_lifecycle::State & previous_state) override
    {
        // 停止定时器，暂停周期性任务
        RCLCPP_INFO(get_logger(), "Node is deactivating.");

        return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS;
    }

    // 清理回调
    rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
    on_cleanup(const rclcpp_lifecycle::State & previous_state) override
    {
        // 释放资源，如销毁定时器等
        RCLCPP_INFO(get_logger(), "Node is cleaning up.");

        return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS;
    }
};

int main(int argc, char ** argv)
{
    rclcpp::init(argc, argv);

    auto node = std::make_shared<TemplateLifecycleNode>();

    rclcpp::spin(node->get_node_base_interface());
    
    rclcpp::shutdown();

    return 0;
}